package com.example.app05.activity;

import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app05.MainActivity;
import com.example.app05.R;
import com.example.app05.dao.MyDatabaseHelper;
import com.example.app05.dao.TaskDao;
import com.example.app05.entity.Task;
import com.example.app05.tool.Tool;

/**
 * @author: cx
 * @date: 2022/5/1
 */
public class DetailsActivity extends AppCompatActivity {

    private Task task;

    //创建数据库所用参数
    private SQLiteDatabase   db;
    private MyDatabaseHelper dbHelper;

    //页面控件
    private TextView tvTopic, tvDoTime, tvShowTime, tvNewTime, tvContent;
    private Button btnBack, btnUpdate, btnDelete;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_details);
        receiveData();
        initDatabaseByDBHelper();
        findView();
        showText();
        setListener();
    }

    private void setListener() {
        //点击返回
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tool.onItemClick(DetailsActivity.this, MainActivity.class,null);
                DetailsActivity.this.finish();
            }
        });
        //点击修改
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tool.onItemClick(DetailsActivity.this,UpdateActivity.class,task);
                DetailsActivity.this.finish();
            }
        });
        //点击删除
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(DetailsActivity.this)
                        .setTitle("温馨提示")
                        .setMessage("是否删除数据")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                TaskDao.deleteData(db,task.getId());
                                Tool.onItemClick(DetailsActivity.this,MainActivity.class,null);
                                DetailsActivity.this.finish();
                            }
                        }).create();
                alertDialog.show();
            }
        });
    }

    private void showText() {
        tvTopic.setText(task.getTopic());
        tvDoTime.setText(task.getDoTime());
        tvShowTime.setText(task.getShowTime());
        tvNewTime.setText(task.getNewTime());
        tvContent.setText(task.getContent());
    }

    private void findView(){
        tvTopic = findViewById(R.id.td_topic);
        tvDoTime = findViewById(R.id.td_doTime);
        tvShowTime = findViewById(R.id.td_showTime);
        tvNewTime = findViewById(R.id.td_updateTime);
        tvContent = findViewById(R.id.td_content);
        btnBack = findViewById(R.id.td_btnBack);
        btnUpdate = findViewById(R.id.td_btnUpdate);
        btnDelete = findViewById(R.id.td_btnDelete);
    }
    //创建数据库
    public void initDatabaseByDBHelper() {
        //创建数据库管理类的对象
        dbHelper = new MyDatabaseHelper(
                this,"task_table",null,1
        );
        //获得数据库对象
        db = dbHelper.getWritableDatabase();
    }
    //接收任务列表页面跳转过来的数据
    private void receiveData(){
        Bundle bundle= (Bundle) getIntent().getExtras().get("bundle");
        task = (Task) bundle.getSerializable("task");
    }
}
